/**
  ******************************************************************************
  * @file    sym32l010_hal_lvd.h
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   Header file of LVD HAL module.
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SYM32L010_HAL_LVD_H
#define __SYM32L010_HAL_LVD_H

#ifdef __cplusplus
extern "C" {
#endif

/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "sym32l010_hal_def.h"


/******************************************************************************/
/* Global type definitions                                                    */
/******************************************************************************/
/* LVD 监测源 -----------------------------------------------------------------*/
typedef enum
{
    LVD_CHANNEL_AVCC           = (0x0UL << LVD_CR0_CHANNEL_Pos),      // AVCC          电源电压
    LVD_CHANNEL_IN1            = (0x1UL << LVD_CR0_CHANNEL_Pos),      // LVD_IN1(PA03) 管脚电压
} LVD_ChannelTypeDef;

/* LVD 阈值电压 ---------------------------------------------------------------*/
typedef enum
{
    LVD_VTH_1V8                = (0x0UL << LVD_CR0_VTH_Pos),          // 阈值电压 1.8V
    LVD_VTH_2V2                = (0x1UL << LVD_CR0_VTH_Pos),          // 阈值电压 2.2V
    LVD_VTH_2V6                = (0x2UL << LVD_CR0_VTH_Pos),          // 阈值电压 2.6V
    LVD_VTH_3V0                = (0x3UL << LVD_CR0_VTH_Pos),          // 阈值电压 3.0V
    LVD_VTH_3V4                = (0x4UL << LVD_CR0_VTH_Pos),          // 阈值电压 3.4V
    LVD_VTH_3V8                = (0x5UL << LVD_CR0_VTH_Pos),          // 阈值电压 3.8V
    LVD_VTH_4V2                = (0x6UL << LVD_CR0_VTH_Pos),          // 阈值电压 4.2V
    LVD_VTH_4V6                = (0x7UL << LVD_CR0_VTH_Pos)           // 阈值电压 4.6V
} LVD_ThresholdTypeDef;

/* LVD 触发动作 ---------------------------------------------------------------*/
typedef enum
{
    LVD_ACTION_RESET           = (0x1UL << LVD_CR0_ACTION_Pos),       // 触发复位
    LVD_ACTION_INT             = (0x0UL << LVD_CR0_ACTION_Pos)        // 触发中断
} LVD_ActionTypeDef;

/* LVD 滤波时钟源 -------------------------------------------------------------*/
typedef enum
{
    LVD_FILTERCLK_LSI          = (0x0UL << LVD_CR0_FLTCLK_Pos),       // 内置低速RC振荡器时钟LSI
    LVD_FILTERCLK_SYSCLK       = (0x1UL << LVD_CR0_FLTCLK_Pos)        // 内部系统时钟
} LVD_FilterClkTypeDef;

/* LVD 滤波时间配置 -----------------------------------------------------------*/
typedef enum
{
    LVD_FILTERTIME_NONE        = (0x0UL << LVD_CR1_FLTTIME_Pos),      // 关闭滤波/无滤波
    LVD_FILTERTIME_FLTCLK1_N2  = (0x1UL << LVD_CR1_FLTTIME_Pos),      // FLTCLK/1  N = 2
    LVD_FILTERTIME_FLTCLK1_N4  = (0x2UL << LVD_CR1_FLTTIME_Pos),      // FLTCLK/1  N = 4
    LVD_FILTERTIME_FLTCLK1_N8  = (0x3UL << LVD_CR1_FLTTIME_Pos),      // FLTCLK/1  N = 8
    LVD_FILTERTIME_FLTCLK2_N6  = (0x4UL << LVD_CR1_FLTTIME_Pos),      // FLTCLK/2  N = 6
    LVD_FILTERTIME_FLTCLK2_N8  = (0x5UL << LVD_CR1_FLTTIME_Pos),      // FLTCLK/2  N = 8
    LVD_FILTERTIME_FLTCLK4_N6  = (0x6UL << LVD_CR1_FLTTIME_Pos),      // FLTCLK/4  N = 6
    LVD_FILTERTIME_FLTCLK4_N8  = (0x7UL << LVD_CR1_FLTTIME_Pos),      // FLTCLK/4  N = 8
    LVD_FILTERTIME_FLTCLK8_N6  = (0x8UL << LVD_CR1_FLTTIME_Pos),      // FLTCLK/8  N = 6
    LVD_FILTERTIME_FLTCLK8_N8  = (0x9UL << LVD_CR1_FLTTIME_Pos),      // FLTCLK/8  N = 8
    LVD_FILTERTIME_FLTCLK16_N5 = (0xAUL << LVD_CR1_FLTTIME_Pos),      // FLTCLK/16 N = 5
    LVD_FILTERTIME_FLTCLK16_N6 = (0xBUL << LVD_CR1_FLTTIME_Pos),      // FLTCLK/16 N = 6
    LVD_FILTERTIME_FLTCLK16_N8 = (0xCUL << LVD_CR1_FLTTIME_Pos),      // FLTCLK/16 N = 8
    LVD_FILTERTIME_FLTCLK32_N5 = (0xDUL << LVD_CR1_FLTTIME_Pos),      // FLTCLK/32 N = 5
    LVD_FILTERTIME_FLTCLK32_N6 = (0xEUL << LVD_CR1_FLTTIME_Pos),      // FLTCLK/32 N = 6
    LVD_FILTERTIME_FLTCLK32_N8 = (0xFUL << LVD_CR1_FLTTIME_Pos)       // FLTCLK/32 N = 8
} LVD_FilterTimeTypeDef;

/* LVD 结构体-----------------------------------------------------------------*/
typedef struct
{
    LVD_ChannelTypeDef           Channel;                             // 监测通道源
    LVD_ThresholdTypeDef         Threshold;                           // 阈值电压
    LVD_ActionTypeDef            Action;                              // 触发动作
    uint8_t                      Trigger;                             // 触发模式
    LVD_FilterClkTypeDef         FilterClk;                           // 滤波时钟源
    LVD_FilterTimeTypeDef        FilterTime;                          // 滤波时间配置
}LVD_InitTypeDef;


/******************************************************************************/
/* Global pre-processor symbols/macros ('#define')                            */
/******************************************************************************/
/* LVD 触发模式 ---------------------------------------------------------------*/
#define LVD_TRIGGER_RISE                (LVD_CR1_RISE_Msk)            // 上升沿触发
#define LVD_TRIGGER_FALL                (LVD_CR1_FALL_Msk)            // 下降沿触发
#define LVD_TRIGGER_LEVEL               (LVD_CR1_LEVEL_Msk)           // 低于阈值电压触发
#define LVD_TRIGGER_NONE                0x0UL                         // 无触发设置

//配置LVD相应输入管脚为模拟功能 ------------------------------------------------*/
#define AFx_LVDIN1_PA03()               (SYM_GPIOA->ANALOG_f.PIN3 = 1)

/* LVD 状态标志 ---------------------------------------------------------------*/
#define LVD_IT_FLAG_INTF                (LVD_SR_INTF_Msk)             // 中断标志
#define LVD_IT_FLAG_FLTV                (LVD_SR_FLTV_Msk)             // 数字滤波器输出状态


/******************************************************************************/
/* Global macro function                                                      */
/******************************************************************************/
/**
  * @brief  获取 LVD 状态标志
  * @param  FLAG : LVD 状态标志
  *                @arg LVD_IT_FLAG_INTF
  *                @arg LVD_IT_FLAG_FLTV
  * @retval
  */
#define HAL_LVD_GET_FLAG(FLAG)          (SYM_LVD->SR & ((uint32_t)(FLAG)))

/**
  * @brief  清除 LVD 状态标志
  * @param  FLAG : LVD 状态标志
  *                @arg LVD_IT_FLAG_INTF
  * @retval
  */
#define HAL_LVD_CLR_FLAG(FLAG)          (SYM_LVD->SR = ~((uint32_t)(FLAG)))


/******************************************************************************/
/* Exported variables ('extern', definition in C source)                      */
/******************************************************************************/


/******************************************************************************/
/* Global function prototypes ('extern', definition in C source)              */
/******************************************************************************/
void    HAL_LVD_Init(LVD_InitTypeDef *InitStruct);
void    HAL_LVD_DeInit(void);
void    HAL_LVD_Enable(void);
void    HAL_LVD_Disable(void);
void    HAL_LVD_Enable_IT(void);
void    HAL_LVD_Disable_IT(void);
uint8_t HAL_LVD_GetOutputValue(void);


#ifdef __cplusplus
}
#endif

#endif /* __SYM32L010_HAL_LVD_H */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
